import time

from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.admin_pu_role_locator import AdminPURoleLocator
from Case.python_ui.Common.commontool import CommonTool


class AdminPuRolePages(Basepage):

    def entry_the_role_page(self):
        doc = 'admin端进入role主页面'
        time.sleep(5)
        for i in range(3):
            try:
                self.wait_elevisble(AdminPURoleLocator.paltform_user_tab, doc)
                self.move_mouse_to_click_element(AdminPURoleLocator.paltform_user_tab, doc)
                self.wait_elevisble(AdminPURoleLocator.role_tab, doc)
                self.move_mouse_to_click_element(AdminPURoleLocator.role_tab, doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue
        self.wait_elevisble(AdminPURoleLocator.page_total, doc)

    def create_role(self):
        doc = 'admin端创建角色'
        time.sleep(5)
        self.refresh_page()
        self.entry_the_role_page()
        for i in range(3):
            try:
                self.wait_elevisble(AdminPURoleLocator.create_role_button, doc)
                break
            except:
                self.refresh_page()
                self.entry_the_role_page()
                continue
        # self.click_element(Rolelocator.role_name)
        self.click_element(AdminPURoleLocator.create_role_button, doc)
        self.wait_elevisble(AdminPURoleLocator.create_role_name, doc)
        role_name = CommonTool.generate_string()
        self.send_keys(AdminPURoleLocator.create_role_name, role_name, doc)
        time.sleep(2)
        self.click_element(AdminPURoleLocator.creat_next_button, doc)
        self.wait_elevisble(AdminPURoleLocator.basic_information_button, doc)
        self.click_element(AdminPURoleLocator.creat_next_button, doc)
        time.sleep(3)
        self.click_element(AdminPURoleLocator.creat_next_button, doc)
        self.wait_elevisble(AdminPURoleLocator.add_success_prompt, doc)
        self.send_keys(AdminPURoleLocator.role_name_search_input, role_name, doc)
        self.click_element(AdminPURoleLocator.role_name_search_button, doc)
        time.sleep(5)
        self.wait_elevisble(AdminPURoleLocator.role_name, doc)
        res = self.get_element(AdminPURoleLocator.role_name, doc).text
        return res, role_name

    def query_role(self, role_name):
        doc = 'admin端查询角色'
        time.sleep(5)
        self.refresh_page()
        self.entry_the_role_page()
        self.wait_elevisble(AdminPURoleLocator.role_name_search_input, doc)
        self.keyboard_event(AdminPURoleLocator.role_name_search_input, doc)
        self.send_keys(AdminPURoleLocator.role_name_search_input, role_name, doc)
        time.sleep(5)
        self.click_element(AdminPURoleLocator.role_name_search_button, doc)
        self.wait_elevisble(AdminPURoleLocator.query_result_element, doc)
        time.sleep(5)
        res = self.get_element(AdminPURoleLocator.role_name, doc).text
        return res

    def delete_role(self, role_name):
        doc = 'admin端删除角色'
        time.sleep(5)
        self.refresh_page()
        self.entry_the_role_page()
        self.query_role(role_name)
        self.wait_elevisble(AdminPURoleLocator.role_operation_button, doc)
        time.sleep(2)
        self.move_mouse_to_click_element(AdminPURoleLocator.role_operation_button, doc)
        time.sleep(2)
        self.wait_elevisble(AdminPURoleLocator.role_delete, doc)
        time.sleep(2)
        self.click_element(AdminPURoleLocator.role_delete, doc)
        time.sleep(2)
        self.wait_elevisble(AdminPURoleLocator.role_I_acknowledge, doc)
        time.sleep(2)
        self.click_element(AdminPURoleLocator.role_I_acknowledge, doc)
        time.sleep(2)
        self.click_element(AdminPURoleLocator.role_ok_confirm, doc)
        self.wait_elevisble(AdminPURoleLocator.add_success_prompt, doc)
        time.sleep(5)
        res = self.get_elements(AdminPURoleLocator.role_name, doc)
        return res

    def edit_role(self, role_name):
        doc = 'admin端编辑角色'
        time.sleep(5)
        self.refresh_page()
        self.query_role(role_name)
        edit_role_name = CommonTool.generate_edit_role_name()
        try:
            time.sleep(5)
            self.move_mouse_to_click_element(AdminPURoleLocator.role_edit, doc)
        except StaleElementReferenceException:
            self.wait_elevisble(AdminPURoleLocator.role_edit, doc)
            self.click_element(AdminPURoleLocator.role_edit, doc)
        self.wait_elevisble(AdminPURoleLocator.edit_clear_input, doc)
        time.sleep(2)
        self.click_element(AdminPURoleLocator.edit_clear_input, doc)
        time.sleep(2)
        self.click_element(AdminPURoleLocator.edit_role_name, doc)
        time.sleep(2)
        self.send_keys(AdminPURoleLocator.edit_role_name, edit_role_name, doc)
        self.click_element(AdminPURoleLocator.role_ok_confirm, doc)
        self.refresh_page()
        self.wait_elevisble(AdminPURoleLocator.role_name_search_input, doc)
        self.send_keys(AdminPURoleLocator.role_name_search_input, edit_role_name, doc)
        self.click_element(AdminPURoleLocator.role_name_search_button, doc)
        time.sleep(5)
        self.wait_elevisble(AdminPURoleLocator.role_name, doc)
        res = self.get_element(AdminPURoleLocator.role_name, doc).text
        return res, edit_role_name

    def edit_role_permissions(self):
        doc = 'admin端编辑角色的权限'
        self.wait_elevisble(AdminPURoleLocator.query_result_element, doc)
        self.wait_elevisble(AdminPURoleLocator.role_operation_button, doc)
        self.move_mouse_to_click_element(AdminPURoleLocator.role_operation_button, doc)
        self.wait_elevisble(AdminPURoleLocator.role_permission_edit, doc)
        self.click_element(AdminPURoleLocator.role_permission_edit, doc)
        time.sleep(5)
        self.click_element(AdminPURoleLocator.bill_permission_edit_next, doc)
        self.wait_elevisble(AdminPURoleLocator.add_success_prompt, doc)

    # 验证列表字段 和 actions列的值
    def list_columns(self):
        doc = "验证admin端role列表字段"

        self.entry_the_role_page()
        self.wait_elevisble(AdminPURoleLocator.list_columns, doc)
        list_columns = self.get_elements(AdminPURoleLocator.list_columns, doc)
        assert list_columns[0].text == "Role Name"
        assert list_columns[1].text == "Role Type"
        assert list_columns[2].text.startswith("Creation Time")
        assert list_columns[3].text == "Actions"

        self.wait_elevisble(AdminPURoleLocator.loading_wait, doc)
        self.wait_eleinvisble(AdminPURoleLocator.loading_wait, doc)

        self.wait_elevisble(AdminPURoleLocator.role_operation_button, doc)
        self.move_mouse_to_click_element(AdminPURoleLocator.role_operation_button, doc)
        self.wait_elevisble(AdminPURoleLocator.role_permission_edit, doc)
        assert self.get_element(AdminPURoleLocator.role_permission_edit_span, doc).text == "Edit Role Permissions"
        assert self.get_element(AdminPURoleLocator.role_delete, doc).text == "Delete"
